﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.IO;
using Meeting.Model;
using Newtonsoft.Json;
using System.Configuration;
using Components;

namespace Meeting.Web.ashx
{
    /// <summary>
    /// Meeting 的摘要说明
    /// </summary>
    public class VoteHandler : IHttpHandler
    {
        string methodName = "";
        bool jsonP = false;
        BLL.Vote bll = new BLL.Vote();

        public void ProcessRequest(HttpContext context)
        {
            string header = "{0}";
            try
            {
                String cb = context.Request.Params["callback"]; //  
                //传过来的 callback 参数 假如 Ext.data.JsonP.callback  
                if (cb != null)
                {
                    jsonP = true;
                    context.Response.ContentType = "text/javascript";
                }
                else
                {
                    context.Response.ContentType = "application/x-json;charset=utf-8";
                }

                header = "";
                if (jsonP)
                {
                    header = cb + "({0})";
                }
                else
                {
                    header = "{0}";
                }
                methodName = context.Request.Params["method"];
                //Model.Vote model = new Model.Vote();
                switch (methodName.ToLower())
                {
                    case "getbymeeting":
                        int meetingId = Convert.ToInt32(context.Request.Params["meetingId"]);
                        context.Response.Write(string.Format(header, bll.GetVotesByMeeting(meetingId)));
                        break;
                    case "getbyemployee":
                        string employeeId = context.Request.Params["employeeId"];
                        context.Response.Write(string.Format(header, bll.GetVotesByEmployee(employeeId)));
                        break;
                    case "getitems":
                        int voteId = Convert.ToInt32(context.Request.Params["voteId"]);
                        context.Response.Write(string.Format(header, bll.GetVoteItemsByVoteId(voteId)));
                        break;
                    case "add":
                        string model = context.Request.Params["model"];
                        meetingId = Convert.ToInt32(context.Request.Params["meetingId"]);
                        context.Response.Write(string.Format(header, bll.AddVote(model, meetingId)));
                        break;
                    case "result":
                        model = context.Request.Params["model"];
                        context.Response.Write(string.Format(header, bll.SubmitResult(model)));
                        break;
                    case "getresult":
                        voteId = Convert.ToInt32(context.Request.Params["voteId"]);
                        meetingId = Convert.ToInt32(context.Request.Params["meetingId"]);
                        context.Response.Write(string.Format(header, bll.GetVoteResult(meetingId,voteId)));
                        break;
                    case "getrelation":
                        voteId = Convert.ToInt32(context.Request.Params["voteId"]);
                        context.Response.Write(string.Format(header, bll.GetVoteAndRelation(voteId)));
                        break;
                    case "result_office":
                        model = context.Request.Params["model"];
                        context.Response.Write(string.Format(header, bll.SubmitResult_Office(model)));
                        break;
                    case "getresult_office":
                        voteId = Convert.ToInt32(context.Request.Params["voteId"]);
                        meetingId = Convert.ToInt32(context.Request.Params["meetingId"]);
                        context.Response.Write(string.Format(header, bll.GetVoteResult_Office(meetingId, voteId)));
                        break;
                }
            }
            catch (Exception ex)
            {
                JsonResponse response = new JsonResponse();
                response.success = false;
                response.Message = ex.Message;
                context.Response.Write(string.Format(header, JsonConvert.SerializeObject(response)));
            }
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}